package yogaService

import (
	"duoke/model"
	"duoke/model/yogaModel"
	"duoke/service/common"
	"errors"
	"fmt"
	"github.com/gin-gonic/gin"
)

var Teacher = new(teacherService)

type teacherService struct{}

func (s *teacherService) PrivateClasses(c *gin.Context, req *yogaModel.SearchGroupClass) (*[]yogaModel.ReqTeacherJoin,
	int64, error) {
	var m []yogaModel.ReqTeacherJoin
	where := fmt.Sprintf("reservation_date=%d and reservation_day=%d and class_type=%d and yt.deleted_at is null",
		req.ReservationDate, req.ReservationDay, req.ClassType)
	count, err := common.Tales.Infos(c, &model.ReqListV2{
		PageSize: req.PageSize,
		PageNo:   req.PageNo,
	}, where, "yt.*").Table("yoga_teacher as yt").
		Join("INNER", "yoga_date as yd", "yt.id=yd.class_id").FindAndCount(&m)
	return &m, count, err
}

func (s *teacherService) Info(c *gin.Context, id int) (*yogaModel.RspTeacher, error) {
	// 获取教师详情
	var m yogaModel.RspTeacher
	get, err := common.Tales.Info(fmt.Sprintf("id=%d", id), "*").
		Table("yoga_teacher").Get(&m.Info)
	if err != nil || !get {
		return nil, errors.New("没有这条数据")
	}
	// 获取教师相册
	where := fmt.Sprintf("from_id=%d and album_type=2", id)
	err = common.Tales.Infos(c, &model.ReqListV2{
		PageSize: 15,
		PageNo:   1,
	}, where, "id,url").Table("yoga_album").Find(&m.Album)
	return &m, err
}

func (s *teacherService) CourseDetails(c *gin.Context, req *model.ReqListV2) (*yogaModel.RspCourseDetails, error) {
	where := fmt.Sprintf("teacher_id=%d and is_recommended=1", req.FromId)
	var list yogaModel.RspCourseDetails
	req.PageSize = 15
	req.PageNo = 1
	err := common.Tales.Infos(c, req, where, "*").Table("yoga_group_class").
		Find(&list.Class)
	if err != nil {
		return nil, err
	}
	get, err := common.Tales.Info(fmt.Sprintf("id=%d", req.FromId), "*").Table("yoga_teacher").
		Get(&list.Teacher)
	if err != nil || !get {
		return nil, errors.New("教练信息不存在")
	}
	return &list, nil
}
